﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;
using System.Linq;
using System.Web.UI;

namespace ud_gSDMS.testing
{
    public partial class Report_Error_Lists : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private int id = -1;
        private string status = string.Empty;
        private SiteUser _siteUser;

        void Page_Load(object sender, EventArgs e)
        {
            try
            {
                //if (!UserCanEditPage() || !UserCanViewPage())
                //{
                //    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
                //}

                if (!Page.IsPostBack)
                {
                    PopulateLabels();
                    PopulateControls();
                }
            }
            catch
            {
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            

            LoadControl();
            LoadGrid();
        }
        private void LoadGrid()
        {
            DataTable data;
            if (status == string.Empty)
            {
                data = gSDMS_BL.ud_gSDMS_PROJECT_TEST_ERROR_Lst(id);
            }
            else
            {
                data = gSDMS_BL.ud_gSDMS_PROJECT_TEST_ERROR_Lst(id , status);
            }
            
            
            if (ViewState["SortExpression"] != null)
            {
                var dv = data.DefaultView;
                dv.Sort = ViewState["SortExpression"].ToString();
                data = dv.Table;
            }

            
            grdError_Lists. DataSource = data;
            grdError_Lists.DataBind();

            ViewState["listError"] = data;

           
        }
        private void LoadControl()
        {
            var temp = gSDMS_BL.ud_gSDMS_PROJECT_TEST_Get(id);

            lbProject_Code.Text = temp.ud_gSDMS_PROJECT.Code;
            lbProject_Name.Text = temp.ud_gSDMS_PROJECT.Name;
            lbStatus.Text = temp.ud_gSDMS_CAT_PROJECT_TEST_STATUS.Name;

            lbTotal_Errors.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                    select d).Count().ToString();
            lbOpen.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                                     where d.ud_gSDMS_CAT_ERROR_STATUS.Name == "Open"
                                                     select d).Count().ToString();
            lbPending.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                             where d.ud_gSDMS_CAT_ERROR_STATUS.Name == "Pending"
                                             select d).Count().ToString();
            lbFixed.Text = (from d in temp.ud_gSDMS_PROJECT_TEST_ERRORs
                                           where d.ud_gSDMS_CAT_ERROR_STATUS.Name == "Closed"
                                           select d).Count().ToString();


             
        }

        private void grdError_Lists_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdError_Lists.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        private void grdError_Lists_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (ViewState["listError"] != null)
            {
                DataTable data = (DataTable)ViewState["listError"];
                int index = int.Parse(e.CommandArgument.ToString());
                var row = data.Rows[index];

  
            }
        }
        void grdError_Lists_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            LoadGrid();
        }
        void grdError_Lists_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
            LoadGrid();
        }

        

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdError_Lists.PageIndexChanging += grdError_Lists_PageIndexChanging;
            grdError_Lists.Sorting += grdError_Lists_Sorting;

            btnExport_Excel.Click += new EventHandler(btnExport_Excel_Click);

            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        void btnExport_Excel_Click(object sender, EventArgs e)
        {
            var s = ViewState["listError"];
            if (s != null)
            {
                hiddenGrid.DataSource = s;
                hiddenGrid.DataBind();

                var name = ("Error-List-" + lbProject_Code.Text);

                hiddenGrid.ExportSettings.FileName = name;
                hiddenGrid.ExportSettings.IgnorePaging = true;
                hiddenGrid.ExportSettings.OpenInNewWindow = true;
                hiddenGrid.MasterTableView.ExportToExcel();

                hiddenGrid.DataSource = null;
                //    ASPxGridViewExporter1.WriteXlsToResponse(name);

            }
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

        }



        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
            id = WebUtils.ParseInt32FromQueryString("id", id);
            status = WebUtils.ParseStringFromQueryString("status", status);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}